# Contributor: ...
# http://aur.archlinux.org/packages.php?ID=37083

pkgname=xbmc-pvr-testing
pkgver=30871
pkgrel=2
pkgdesc="XBMC Media Center with PVR support (VDR ecc..)"
arch=('i686' 'x86_64')
url="https://xbmc.svn.sourceforge.net/svnroot/xbmc/branches/pvr-testing2/"
license=('GPL' 'LGPL')
# libva-sds
depends=('alsa-lib' 'curl' 'enca' 'faac' 'freetype2' 'fribidi' 'gawk' 'glew'
         'jasper' 'libgl' 'libjpeg>=7' 'libpng>=1.4' 'libmad' 'libmysqlclient'
         'libxinerama' 'libxrandr' 'lzo2' 'sdl_image>=1.2.10' 'sdl_mixer' 'sqlite3'
         'tre' 'unzip' 'xorg-server' 'libcdio' 'smbclient' 'faad2' 'libsamplerate' 
         'libmms' 'xorg-utils' 'wavpack' 'libmicrohttpd' 'libmpeg2' 'libmodplug'
         'libssh2' 'glew' 'upower' 'udisks' 'dbus' 'cvs')
# libva-sds
makedepends=('subversion' 'autoconf' 'automake' 'boost' 'cmake' 'gcc' 'gperf' 
             'libtool>=2.2.6a-1' 'make' 'nasm' 'patch' 'pkgconfig' 'zip' 'flex' 
             'bison' 'libvdpau' 'glew')
optdepends=('lirc: remote controller support'
            'smbclient: access windows shares'
            'gdb: for meaningful backtraces in case of trouble - STRONGLY RECOMMENDED'
            'unrar: access compressed files without unpacking them')
provides=('xbmc')
conflicts=('xbmc' 'xbmc-crystalhd' 'xbmc-trunk' 'xbmc-pulse')
install=("${pkgname}.install")
source=(FEH.sh 
        )
md5sums=('c3e2ab79b9965f1a4a048275d5f222c4'
        )

_svnmod=XBMC
_prefix=/usr

build() {
    #MAKEFLAGS="-j1"

    _svntrunk=https://xbmc.svn.sourceforge.net/svnroot/xbmc/branches/pvr-testing2/

    cd ${srcdir}/
    if [ -d $_svnmod/.svn ]; then
        (cd $_svnmod && svn up -r $pkgver) || return 1
    else
        svn co $_svntrunk --config-dir ./ -r $pkgver $_svnmod || return 1
    fi
    msg "SVN checkout done or server timeout"
    msg "Starting make..."

  if [ -d "$srcdir/$_svnmod-build" ]; then
    rm -r "$srcdir/$_svnmod-build"
  fi

    cp -r "$srcdir/$_svnmod" "$srcdir/$_svnmod-build"
    cd "$srcdir/$_svnmod-build"

    
    # Patch for missing projectM presets
   #patch -p0 -i ../projectM.diff || return 1


    # Archlinux Branding by SVN_REV
    export SVN_REV="$pkgver-ARCH"

    # fix lsb_release dependency
    sed -i -e 's:/usr/bin/lsb_release -d:cat /etc/arch-release:' xbmc/utils/SystemInfo.cpp || return 1



# SSE2 -> SSE
msg "SSE2 -> SSE"
sed -i 's/CXXFLAGS+=-I. -DSQUISH_USE_SSE=2 -msse2/CXXFLAGS+=-I. -DSQUISH_USE_SSE=1 -msse/' ./xbmc/lib/libsquish/Makefile.in



    # Configure XBMC
    msg "Bootstrapping XBMC"
    ./bootstrap || return 1

    msg "Configuring XBMC" 
     ./configure  --prefix=${_prefix} \
                  --disable-hal \
                  --enable-external-libraries \
                  --enable-external-libass \
                  --disable-external-ffmpeg \
                  --disable-external-python \
                  --enable-debug || return 1

  # Now (finally) build
  msg "Running make" 
  make || return 1
}

package() {
  cd "${srcdir}/${_svnmod}-build"

  msg "Running make install" 
  make prefix=${pkgdir}${_prefix} install || return 1

  # Replace FEH.py with FEH.sh (and thus remove external python dependency)
  install -D -m 0755 ${srcdir}/FEH.sh ${pkgdir}${_prefix}/share/xbmc/FEH.sh || return 1

  sed -i -e 's/^python \(.*\)FEH.py \(.*\)$/\1FEH.sh \2/' ${pkgdir}${_prefix}/bin/xbmc || return 1

  # lsb_release fix
  sed -i -e 's/which lsb_release &> \/dev\/null/\[ -f \/etc\/arch-release ]/g' ${pkgdir}${_prefix}/bin/xbmc || return 1

  sed -i -e "s/lsb_release -a 2> \/dev\/null | sed -e 's\/\^\/    \/'/cat \/etc\/arch-release/g" ${pkgdir}${_prefix}/bin/xbmc || return 1

  # .desktop files
  install -D -m 0644 ${srcdir}/${_svnmod}-build/tools/Linux/xbmc.desktop ${pkgdir}${_prefix}/share/applications/xbmc.desktop || return 1
  install -D -m 0644 ${srcdir}/${_svnmod}-build/tools/Linux/xbmc.png ${pkgdir}${_prefix}/share/pixmaps/xbmc.png || return 1

  # Tools
  install -D -m 0755 ${srcdir}/${_svnmod}-build/xbmc-xrandr ${pkgdir}${_prefix}/share/xbmc/xbmc-xrandr || return 1

  install -D -m 0755 ${srcdir}/${_svnmod}-build/tools/TexturePacker/TexturePacker ${pkgdir}${_prefix}/share/xbmc/ || return 1

  # License(s)
  install -dm755 ${pkgdir}/usr/share/licenses/${pkgname}
  for licensef in LICENSE.GPL copying.txt; do 
    mv ${pkgdir}${_prefix}/share/doc/${licensef} ${pkgdir}/usr/share/licenses/${pkgname} || return 1 
  done

  # Docs 
  install -d -m 0755 ${pkgdir}${_prefix}/share/doc/${pkgname} 
  for docsf in keymapping.txt README.linux; do 
    mv ${pkgdir}${_prefix}/share/doc/${docsf} ${pkgdir}/usr/share/doc/${pkgname} || return 1 
  done

  # cleanup some stuff
  msg "Cleanup unneeded files"
  rm -rf ${pkgdir}/usr/share/xsessions
  rm -f ${pkgdir}/usr/share/xbmc/FEH.py

  # strip
  msg "Stripping binaries"
  find $pkgdir -type f -exec strip {} \; >/dev/null 2>/dev/null

  # use old dir scheme
  mv ${pkgdir}${_prefix}/lib/xbmc/* ${pkgdir}${_prefix}/share/xbmc/ || return 1

  rm -rf ${pkgdir}${_prefix}/lib/xbmc

  # fix LIBDIR in /usr/bin/xbmc
  sed -e 's/LIBDIR="\/usr\/lib/LIBDIR="\/usr\/share/g' -i ${pkgdir}${_prefix}/bin/xbmc
}
